// 清空购物车
function clearCart() {
    document.cookie = "cart=;expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    location.reload();
}

// 刷新页面
function refreshPage() {
    location.reload();
}
function removeItem(name) {
    let cart = [];
    if (document.cookie.includes('cart=')) {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.startsWith('cart=')) {
                const cartJSON = decodeURIComponent(cookie.substring(5));
                cart = JSON.parse(cartJSON);
                break;
            }
        }
    }
    // 查找并移除商品名称
    const index = cart.indexOf(name);
    if (index !== -1) {
        cart.splice(index, 1);
        const cartJSON = JSON.stringify(cart);
        document.cookie = 'cart=' + encodeURIComponent(cartJSON) + '; expires=Thu, 01 Jan 2024 00:00:00 UTC; path=/;';

        window.location.reload();
    }
}
function settleCount(price){
    const SettleCount = "SettleCount.jsp?totalPrice=" + price;
    window.open(SettleCount,"_blank","width=400px,height=650px");
}

function dropAndRiseAnimation2(message, button) {
    removeItem(message);
    const animationBox = document.getElementById("animationBox");
    const animationText = document.getElementById("animationText");

    // 计算按钮位置
    const buttonRect = button.getBoundingClientRect();
    const buttonTop = buttonRect.top + window.scrollY;
    const buttonLeft = buttonRect.left + window.scrollX;

    // 显示动画元素
    animationBox.style.display = "block";

    // 设置动态生成的文字
    animationText.textContent = '尊敬的客户,商品 ' + name + ' 已从购物车中移除';
    //
    // // 设置动画元素位置
    animationBox.style.top = (buttonTop - 100) + "px";
    animationBox.style.left = (buttonLeft - 100) + "px";

    // 添加动画效果
    animationBox.style.animation = "dropAndRise 5s";

    // 在动画结束后隐藏动画元素，并重置动画
    setTimeout(function () {
        animationBox.style.display = "none";
        animationBox.style.animation = "";
    }, 5000);
}
